package com.hsogoo.study.spring.propertysource;

import java.util.Map;

import org.junit.Test;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePropertySource;

import com.google.common.collect.Maps;

/**
 * Created by za-huangsaigang on 2018/9/17.
 */
public class PropertySourcesPropertyResolverTest {

    @Test
    public void testPropertySourcesPropertyResolver() throws Exception{
        Map<String, Object> map = Maps.newHashMap();
        map.put("school", "shanghai");
        map.put("class", "man");

        MapPropertySource source1 = new MapPropertySource("student", map);

        Resource resource = new ClassPathResource("application.properties");

        ResourcePropertySource source2 = new ResourcePropertySource("person", resource);

        SystemEnvironmentPropertySource source3 = new SystemEnvironmentPropertySource("systemEnvironment",(Map)System.getenv());
        MutablePropertySources sources = new MutablePropertySources();
        sources.addLast(source1);
        sources.addLast(source2);
        sources.addLast(source3);

        PropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
        System.out.println(resolver.containsProperty("name"));
        System.out.println(resolver.getProperty("school"));
        System.out.println(resolver.getProperty("os"));
        System.out.println(resolver.resolvePlaceholders("My name is ${name} .I am ${age}. School is ${school}"));

    }
}
